﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.LoaiNguonNuocForm;
using ComputerVision.PhanLoaiChiTietNguonNuocForm;
using ComputerVision.Services;
using ComputerVision.Services.User;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.PhieuDieuTraCoSoForm
{
    public partial class frmNguonNuoc : frmDetail
    {
        private readonly IService<PhanLoaiChiTietNguonNuoc> _phanLoaiChiTietNguonNuocService;
        private readonly IService<LoaiNguonNuoc> _loaiNguonNuocService;
        private readonly IService<NguonNuoc> _nguonNuocService;
        private readonly IUserService<aspnet_Users> _userService;
        public Guid IdPhieu;

        public frmNguonNuoc()
        {
            InitializeComponent();
            _phanLoaiChiTietNguonNuocService = Program.Container.Resolve<IService<PhanLoaiChiTietNguonNuoc>>();
            _loaiNguonNuocService = Program.Container.Resolve<IService<LoaiNguonNuoc>>();
            _nguonNuocService = Program.Container.Resolve<IService<NguonNuoc>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm Nguồn nước";
            LoadLoaiNguonNuoc();
        }

        public frmNguonNuoc(Guid id)
        {
            InitializeComponent();
            _phanLoaiChiTietNguonNuocService = Program.Container.Resolve<IService<PhanLoaiChiTietNguonNuoc>>();
            _loaiNguonNuocService = Program.Container.Resolve<IService<LoaiNguonNuoc>>();
            _nguonNuocService = Program.Container.Resolve<IService<NguonNuoc>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            LoadLoaiNguonNuoc();
            Text = @"Sửa Nguồn nước";

            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            var item = _nguonNuocService.Detail(id);
            if (item != null)
            {
                drlLoaiNguonNuoc.SelectedValue = item.PhanLoaiChiTietNguonNuoc.LoaiNguonNuocId;
                LoadPhanLoaiChitiet(item.PhanLoaiChiTietNguonNuoc.LoaiNguonNuocId);

                txtTen.Text = item.Ten;
                txtTrongCoSo.Text = item.TrongCoSo;
                txtNgoaiCoSo.Text = item.NgoaiCoSo;
                txtKhoiTich.Text = item.KhoiTich;
                txtXeCC.Text = item.XeCC;
                txtMBCC.Text = item.MBCC;
                drlPhanLoaiChiTietNguonNuoc.SelectedValue = item.PhanLoaiChiTietNguonNuocId;
            }
        }

        public void LoadLoaiNguonNuoc()
        {
            drlLoaiNguonNuoc.DataSource = _loaiNguonNuocService.ToList().ToList();
            drlLoaiNguonNuoc.DisplayMember = "Ten";
            drlLoaiNguonNuoc.ValueMember = "Id";
        }

        public void LoadPhanLoaiChitiet(Guid? idLoai)
        {
            drlPhanLoaiChiTietNguonNuoc.DataSource = _phanLoaiChiTietNguonNuocService.ToList().ToList().Where(p => p.LoaiNguonNuocId.Equals(idLoai)).ToList();
            drlPhanLoaiChiTietNguonNuoc.DisplayMember = "Ten";
            drlPhanLoaiChiTietNguonNuoc.ValueMember = "Id";
        }

        public void LoadPhanLoaiChitiet()
        {
            Guid idLoai = Guid.Parse(drlLoaiNguonNuoc.SelectedValue.ToString());
            drlPhanLoaiChiTietNguonNuoc.DataSource = _phanLoaiChiTietNguonNuocService.ToList().ToList().Where(p => p.LoaiNguonNuocId.Equals(idLoai)).ToList();
            drlPhanLoaiChiTietNguonNuoc.DisplayMember = "Ten";
            drlPhanLoaiChiTietNguonNuoc.ValueMember = "Id";
        }

        private void btnAddLoaiNguonNuoc_Click(object sender, EventArgs e)
        {
            var frm = new frmLoaiNguonNuocDetail(this);
            frm.ShowDialog();
        }

        private void btnAddPhanLoaiChitiet_Click(object sender, EventArgs e)
        {
            var frm = new frmPhanLoaiChiTietNguonNuocDetail(this);
            frm.ShowDialog();
        }

        private void drlLoaiNguonNuoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadPhanLoaiChitiet(Guid.Parse(drlLoaiNguonNuoc.SelectedValue.ToString()));
        }

        #region Validation

        protected override bool Validator()
        {
            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtTen.Text))
            {
                errorProvider.SetError(txtTen, "Bạn chưa nhập tên nguồn nước. Xin vui lòng kiểm tra lại");
                return false;
            }

            if (drlPhanLoaiChiTietNguonNuoc.SelectedValue == null)
            {
                errorProvider.SetError(drlPhanLoaiChiTietNguonNuoc, "Bạn chưa chọn phân loại chi tiết. Xin vui lòng kiểm tra lại");
                return false;
            }

            return true;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var model = new NguonNuoc()
                {
                    Id = Guid.NewGuid(),
                    Ten = txtTen.Text,
                    PhanLoaiChiTietNguonNuocId = Guid.Parse(drlPhanLoaiChiTietNguonNuoc.SelectedValue.ToString()),
                    PhieuDieuTraCoSoesId = this.IdPhieu,
                    TrongCoSo = txtTrongCoSo.Text,
                    NgoaiCoSo = txtNgoaiCoSo.Text,
                    KhoiTich = txtKhoiTich.Text,
                    XeCC = txtXeCC.Text,
                    MBCC = txtMBCC.Text,
                    Created = DateTime.Now,
                    CreatedBy = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy = Constants.CurrentUser
                };
                _nguonNuocService.Create(model);
            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var item = _nguonNuocService.Detail(guid);
            if (item != null)
            {
                item.Ten = txtTen.Text;
                item.PhanLoaiChiTietNguonNuocId = Guid.Parse(drlPhanLoaiChiTietNguonNuoc.SelectedValue.ToString());
                //item.PhieuDieuTraCoSoesId = IdPhieu;
                item.TrongCoSo = txtTrongCoSo.Text;
                item.NgoaiCoSo = txtNgoaiCoSo.Text;
                item.KhoiTich = txtKhoiTich.Text;
                item.XeCC = txtXeCC.Text;
                item.MBCC = txtMBCC.Text;
                item.Modified = DateTime.Now;
                item.ModifiedBy = Constants.CurrentUser;
                _nguonNuocService.Update(item);
            }
        }

        #endregion
    }
}
